package com.dahaiwuliang.code.server.impl;

import com.dahaiwuliang.code.dao.UserDao;
import com.dahaiwuliang.code.pojo.User;
import com.dahaiwuliang.code.server.UserServer;
import com.dahaiwuliang.code.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

@Service
public class UserServerImpl implements UserServer {

    @Autowired
    private UserDao userDao;

    @Override
    public User findByUserName(String userName) {
        return userDao.findByUserName(userName);
    }

    @Override
    public User findByEmail(String email) {
        return userDao.findByEmail(email);
    }

    @Override
    public void save(User user) {
        userDao.save(user);

    }

    @Override
    public User findById(Integer userId) {
        return userDao.getOne(userId);
    }

    @Override
    public Long getCount(User s_user, String s_blatelyLoginTime, String s_elatelyLoginTime) {
        return userDao.count(new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate predicate=criteriaBuilder.conjunction();
                if (StringUtil.isNotEmpty(s_blatelyLoginTime)){
                    predicate.getExpressions().add(criteriaBuilder.greaterThanOrEqualTo(root.get("latelyLoginTime").as(String.class),s_blatelyLoginTime));
                }
                if (StringUtil.isNotEmpty(s_elatelyLoginTime)){
                    predicate.getExpressions().add(criteriaBuilder.lessThanOrEqualTo(root.get("latelyLoginTime").as(String.class),s_elatelyLoginTime));

                }
                if (s_user!=null){
                    if (StringUtil.isNotEmpty(s_user.getSex())){
                        predicate.getExpressions().add(criteriaBuilder.equal(root.get("sex"),s_user.getSex()));

                    }
                    if (StringUtil.isNotEmpty(s_user.getUserName())){
                        predicate.getExpressions().add(criteriaBuilder.like(root.get("userName"),"%"+s_user.getSex()+"%"));

                    }
                }
                return predicate;


            }
        });
    }

    @Override
    public Integer todayRegister() {
        return userDao.todayRegister();
    }

    @Override
    public Integer todayLogin() {
        return userDao.todayLogin();
    }

    @Override
    public Integer Register() {
        return userDao.Register();
    }

    @Override
    public List<User> list(User s_user, String s_blatelyLoginTimes, String s_elatelyLoginTimes, Integer page, Integer pageSize, Sort.Direction direction, String... properties) {
        Page<User> userPage=userDao.findAll(new Specification<User>() {
            @Override
            public Predicate toPredicate(Root<User> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate predicate=criteriaBuilder.conjunction();
                if (StringUtil.isNotEmpty(s_blatelyLoginTimes)){
                    predicate.getExpressions().add(criteriaBuilder.greaterThanOrEqualTo(root.get("latelyLoginTime").as(String.class),s_blatelyLoginTimes));
                }
                if (StringUtil.isNotEmpty(s_elatelyLoginTimes)){
                    predicate.getExpressions().add(criteriaBuilder.lessThanOrEqualTo(root.get("latelyLoginTime").as(String.class),s_elatelyLoginTimes));

                }
                if (s_user!=null){
                    if (StringUtil.isNotEmpty(s_user.getSex())){
                        predicate.getExpressions().add(criteriaBuilder.equal(root.get("sex"),s_user.getSex()));

                    }
                    if (StringUtil.isNotEmpty(s_user.getUserName())){
                        predicate.getExpressions().add(criteriaBuilder.like(root.get("userName"),"%"+s_user.getSex()+"%"));

                    }
                }
                return predicate;
            }
        },PageRequest.of(page-1,pageSize,direction,properties));

        return userPage.getContent();
    }
}
